package com.lzt.admin.common.redis.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

@Configuration
@RequiredArgsConstructor
public class RedisCore {
    private  final StringRedisTemplate redisTemplate;
    private final ObjectMapper objectMapper;

    /**
     * 缓存对象，Integer、String、实体类等
     *
     * @param key 缓存的键值
     * @param value 缓存的值
     */
    public <T> void setCacheObject(final String key, final T value) throws JsonProcessingException {
        try {

            redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value));
        }
        catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }

    }
    /**
     * 缓存基本的对象，Integer、String、实体类等
     *
     * @param key 缓存的键值
     * @param value 缓存的值
     * @param timeout 时间
     * @param timeUnit 时间颗粒度
     */
    public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) throws JsonProcessingException {
        try {
            redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value), timeout, timeUnit);
        }
        catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }

    }
    /**
     * 获得缓存的对象。
     *
     * @param key 缓存键值
     * @return 缓存键值对应的数据
     */
    public String getCacheObject(final String key) {
        try {
            String str = redisTemplate.opsForValue().get(key);
            if(StrUtil.isNotBlank(str)){
                return str;
//                return JSONUtil.toBean(str,String.class);
            }
            return null;
        }
        catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }

    }
    /**
     * 获得缓存的对象。
     *
     * @param key 缓存键值
     * @return 缓存键值对应的数据
     */
    public <T> T getCacheObject(final String key,Class<T> t) throws JsonProcessingException {
        try {
            String value = redisTemplate.opsForValue().get(key);
            if(StrUtil.isNotBlank(value)) {
                return JSONUtil.toBean(value, t);
            }
            return null;
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    /**
     * 删除单个对象
     *
     * @param key
     */
    public boolean deleteObject(final String key)
    {
        return redisTemplate.delete(key);
    }
    /**
     * 判断 key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public Boolean hasKey(String key)
    {
        return redisTemplate.hasKey(key);
    }
    public Collection<String> keys(final String pattern)
    {
        return redisTemplate.keys(pattern);
    }
}
